﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace T4UtilityBelt.Serializers
{
    [AttributeUsage(AttributeTargets.Property)]
    public sealed class XSpecifiedValueAttribute : Attribute
    {
        public string XmlDelegate { get; private set; }

        public XSpecifiedValueAttribute(string xmlDelegate)
        {
            XmlDelegate = xmlDelegate;
        }

        public bool IsValueSpecified(object value)
        {
            if (value != null
                && XmlDelegate != null)
            {
                Type t = value.GetType();
                PropertyInfo pi = t.GetProperty(XmlDelegate);
                if (pi != null)
                {
                    return (bool)pi.GetValue(value);
                }
                else
                {
                    throw new SerializerException("XSpecifiedValueAttribute: " + XmlDelegate + " not found on " + t.FullName);        
                }
            }
            
            return true;
        }
    }
}
